﻿using UnityEngine;
using System.Collections;

public class MagicCube : MonoBehaviour
{
    private GameObject[] cubes;     //所有的魔方
    private Vector3 rotateAxis;     //旋转轴
    private Ray ray;                //检测射线
    private RaycastHit hit;     //检测射线信息
    private Transform hitCube;      //鼠标按下时的魔方
    private Vector3 hitCubePos;     //鼠标按下时的魔方位置
    private Vector3 normal;            //鼠标按下时的魔方的法向量
    private Vector3 moveDir;           //
    private Vector3 cross;              //差乘求旋转轴(有误差)
    private bool isRotate;              //旋转标示位


    void Awake()
    {
        cubes = GameObject.FindGameObjectsWithTag("MagicCube");
    }

    void Update()
    {
        Debug.Log(333);

        RotateInspector();
        if (isRotate)
        {
            Rotate();
        }
    }
    //旋转检测
    private void RotateInspector()
    {
        Debug.Log(222);

        //按下
        if (Input.GetMouseButtonDown(0) && !isRotate)
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {

                hitCube = hit.collider.transform;
                hitCubePos = hit.point;
                normal = hit.normal;
            }
        }
        //按住
        if (Input.GetMouseButton(0) && hitCube)
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                moveDir = hit.point - hitCubePos;
                if (moveDir.sqrMagnitude > 0.3f)
                {
                    Debug.Log(111);
                    //差乘求旋转轴
                    cross = Vector3.Cross(normal, moveDir).normalized;
                    SetRotateAxits(cross);
                    hitCube = null;
                }
            }
        }

    }

    //枚举所有可能的旋转轴
    private Vector3[] axis = new Vector3[]
    {
        Vector3.left, Vector3.right,
        Vector3.up, Vector3.down,
        Vector3.forward, Vector3.back
    };


    private void SetRotateAxits(Vector3 cross)
    {
        for (int i = 0; i < axis.Length; i++)
        {
            float dot = Vector3.Dot(cross, axis[i]);
            if (dot >= 0.75 && dot <= 1)
            {
                rotateAxis = axis[i];
                //确定x,y,z
                for (int j = 0; j < 3; j++)
                {
                    if (Mathf.Abs(rotateAxis[j]) == 1)
                    {
                        FindRotateCube(j);
                        return;
                    }
                }
            }
        }
    }

    //查找所有要旋转的cube
    void FindRotateCube(int index)
    {
        //根据鼠标点击点,求参照位置
        float pos = hitCube.position[index];
        for (int i = 0; i < cubes.Length; i++)
        {
            float cubePos = cubes[i].transform.position[index];
            if (Mathf.Abs(cubePos - pos) < 0.1f)
            {
                cubes[i].transform.SetParent(transform);
            }
        }
        isRotate = true;
    }

    private float timer;
    private Quaternion dirRotation;
    //旋转
    private void Rotate()
    {
        timer += Time.deltaTime;
        timer = Mathf.Clamp01(timer);
        transform.rotation = Quaternion.Euler(rotateAxis*timer*90);
        if (timer >= 1)
        {
            isRotate = false;
            timer = 0;
                for (int i = 0; i < cubes.Length; i++)
                {
                    cubes[i].transform.SetParent(transform.parent);
                }
            transform.rotation = Quaternion.identity;
        }
    }
}
